import request from '../../utils/index'
// pages/chooseAddress/chooseAddress.js
let baseUrl = 'http://120.78.172.212:9001'

var date = new Date()
var currentHours = date.getHours()
var currentMinute = date.getMinutes()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 地址数组
    addressList: [],
    // 展示的当前位置的名称
    locationAdd: '',
    // 当前选中的默认地址索引
    chooseIndex: 0,
    //时间选择器数组
    multiArray: [
      ['今天', '明天', '3-2', '3-3', '3-4', '3-5'],
      [0, 1, 2, 3, 4, 5, 6],
      [0, 10, 20],
    ],
    multiIndex: [0, 0, 0],
    //时间选择器内容
    startDate: '请选择送达时间 >',
  },
  // 获取当前地址，反解析为地址名
  getLocation() {
    wx.getLocation({
      type: 'gcj02',
      success: (location) => {
        let locationString = location.latitude + ',' + location.longitude
        var reqTask = wx.request({
          url: 'https://apis.map.qq.com/ws/geocoder/v1',
          data: {
            key: 'FMOBZ-R4NK3-7SH3S-325H2-G5HSK-HEFCV',
            location: locationString,
          },
          method: 'GET',
          success: (res) => {
            // console.log(res)
            this.setData({
              locationAdd: res.data.result.formatted_addresses.recommend,
            })
          },
        })
      },
    })
  },
  // 跳转到添加地址
  toNewAdd() {
    let mode = 'POST'

    wx.navigateTo({
      url: '../newAddress/newAddress?mode=' + mode,
      success: (result) => {},
      fail: () => {},
      complete: () => {},
    })
  },
  // 页面加载函数
  onLoad: function (options) {
    this.getAddr()
    this.getLocation()
  },
  // 获取用户所有地址
  getAddr() {
    request(
      {
        url: baseUrl + '/api/address?limit=100&&page=1',
      },
      (res) => {
        // console.log(res)
        let addressList = res.data.rows
        addressList.map((x) => {
          x.left = 0
          return x
        })
        this.setData({
          addressList,
        })
      }
    )
  },
  // 修改用户信息
  modifyAdd(e) {
    let id = e.currentTarget.dataset.id
    let mode = 'PUT'
    wx.navigateTo({
      url: '../newAddress/newAddress?mode=' + mode + '&&id=' + id,
      success: (result) => {},
      fail: () => {},
      complete: () => {},
    })
  },
  // 开始滑动
  deleteStart(e) {
    // console.log(e)
    this.setData({
      deleteClientX: e.changedTouches[0].clientX,
    })
    // console.log(this.data.deleteClientX);
  },
  // 滑动完成
  deleteMove(e) {
    // console.log(e);
    let moveDis = this.data.deleteClientX - e.changedTouches[0].clientX
    let index = e.currentTarget.dataset.index
    let addressList = this.data.addressList
    if (moveDis > 60) {
      addressList.map((x) => {
        x.left = 0
        return x
      })
      addressList[index].left = -220
    } else if (moveDis < 0) {
      addressList.map((x) => {
        x.left = 0
        return x
      })
    }
    this.setData({
      addressList,
    })
  },
  // 删除地址
  deleteAdd(e) {
    // console.log(e);
    let id = e.currentTarget.dataset.id
    request(
      {
        url: baseUrl + '/api/address',
        data: {
          id,
        },
        method: 'DELETE',
      },
      (res) => {
        console.log(res)
        if (res.success == true) {
          this.getAddr()
        }
      }
    )
  },
  //时间选择器点击事件
  pickerTap() {
    date = new Date()

    var monthDay = ['今天', '明天']
    var hours = []
    var minute = []

    currentHours = date.getHours()
    currentMinute = date.getMinutes()

    // 月-日
    for (var i = 2; i <= 28; i++) {
      var date1 = new Date(date)
      date1.setDate(date.getDate() + i)
      var md = date1.getMonth() + 1 + '-' + date1.getDate()
      monthDay.push(md)
    }

    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex,
    }

    if (data.multiIndex[0] === 0) {
      if (data.multiIndex[1] === 0) {
        this.loadData(hours, minute)
      } else {
        this.loadMinute(hours, minute)
      }
    } else {
      this.loadHoursMinute(hours, minute)
    }

    data.multiArray[0] = monthDay
    data.multiArray[1] = hours
    data.multiArray[2] = minute

    this.setData(data)
  },
  // 切换选项事件
  bindMultiPickerColumnChange(e) {
    date = new Date()

    var that = this

    var monthDay = ['今天', '明天']
    var hours = []
    var minute = []

    currentHours = date.getHours()
    currentMinute = date.getMinutes()

    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex,
    }
    // 把选择的对应值赋值给 multiIndex
    data.multiIndex[e.detail.column] = e.detail.value

    // 然后再判断当前改变的是哪一列,如果是第1列改变
    if (e.detail.column === 0) {
      // 如果第一列滚动到第一行
      if (e.detail.value === 0) {
        that.loadData(hours, minute)
      } else {
        that.loadHoursMinute(hours, minute)
      }

      data.multiIndex[1] = 0
      data.multiIndex[2] = 0

      // 如果是第2列改变
    } else if (e.detail.column === 1) {
      // 如果第一列为今天
      if (data.multiIndex[0] === 0) {
        if (e.detail.value === 0) {
          that.loadData(hours, minute)
        } else {
          that.loadMinute(hours, minute)
        }
        // 第一列不为今天
      } else {
        that.loadHoursMinute(hours, minute)
      }
      data.multiIndex[2] = 0

      // 如果是第3列改变
    } else {
      // 如果第一列为'今天'
      if (data.multiIndex[0] === 0) {
        // 如果第一列为 '今天'并且第二列为当前时间
        if (data.multiIndex[1] === 0) {
          that.loadData(hours, minute)
        } else {
          that.loadMinute(hours, minute)
        }
      } else {
        that.loadHoursMinute(hours, minute)
      }
    }
    data.multiArray[1] = hours
    data.multiArray[2] = minute
    this.setData(data)
  },
  // 载入日期
  loadData(hours, minute) {
    var minuteIndex
    if (currentMinute > 0 && currentMinute <= 10) {
      minuteIndex = 10
    } else if (currentMinute > 10 && currentMinute <= 20) {
      minuteIndex = 20
    } else if (currentMinute > 20 && currentMinute <= 30) {
      minuteIndex = 30
    } else if (currentMinute > 30 && currentMinute <= 40) {
      minuteIndex = 40
    } else if (currentMinute > 40 && currentMinute <= 50) {
      minuteIndex = 50
    } else {
      minuteIndex = 60
    }

    if (minuteIndex == 60) {
      // 时
      for (var i = currentHours + 1; i < 24; i++) {
        hours.push(i)
      }
      // 分
      for (var i = 0; i < 60; i += 10) {
        minute.push(i)
      }
    } else {
      // 时
      for (var i = currentHours; i < 24; i++) {
        hours.push(i)
      }
      // 分
      for (var i = minuteIndex; i < 60; i += 10) {
        minute.push(i)
      }
    }
  },
  // 载入小时分钟
  loadHoursMinute: function (hours, minute) {
    // 时
    for (var i = 0; i < 24; i++) {
      hours.push(i)
    }
    // 分
    for (var i = 0; i < 60; i += 10) {
      minute.push(i)
    }
  },
  // 载入分钟
  loadMinute: function (hours, minute) {
    var minuteIndex
    if (currentMinute > 0 && currentMinute <= 10) {
      minuteIndex = 10
    } else if (currentMinute > 10 && currentMinute <= 20) {
      minuteIndex = 20
    } else if (currentMinute > 20 && currentMinute <= 30) {
      minuteIndex = 30
    } else if (currentMinute > 30 && currentMinute <= 40) {
      minuteIndex = 40
    } else if (currentMinute > 40 && currentMinute <= 50) {
      minuteIndex = 50
    } else {
      minuteIndex = 60
    }

    if (minuteIndex == 60) {
      // 时
      for (var i = currentHours + 1; i < 24; i++) {
        hours.push(i)
      }
    } else {
      // 时
      for (var i = currentHours; i < 24; i++) {
        hours.push(i)
      }
    }
    // 分
    for (var i = 0; i < 60; i += 10) {
      minute.push(i)
    }
  },
  // 时间选择器确认
  bindStartMultiPickerChange(e) {
    var that = this
    var monthDay = that.data.multiArray[0][e.detail.value[0]]
    var hours = that.data.multiArray[1][e.detail.value[1]]
    var minute = that.data.multiArray[2][e.detail.value[2]]

    if (monthDay === '今天') {
      var month = date.getMonth() + 1
      var day = date.getDate()
      monthDay = month + '月' + day + '日'
    } else if (monthDay === '明天') {
      var date1 = new Date(date)
      date1.setDate(date.getDate() + 1)
      monthDay = date1.getMonth() + 1 + '月' + date1.getDate() + '日'
    } else {
      var month = monthDay.split('-')[0] // 返回月
      var day = monthDay.split('-')[1] // 返回日
      monthDay = month + '月' + day + '日'
    }

    var startDate = monthDay + ' ' + hours + ':' + minute
    that.setData({
      startDate: startDate,
    })
  },
})
